package com.xmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xmall.bean.table.Comment;
import com.xmall.bean.table.CommentExample;
import com.xmall.bean.table.User;
import com.xmall.bean.vo.CommentVO;
import com.xmall.bean.vo.MallCommentDataVO;
import com.xmall.bean.vo.MallCommentVO;
import com.xmall.mapper.CommentMapper;
import com.xmall.mapper.UserMapper;
import com.xmall.service.CommentService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Connor
 * @ClassName: CommentServiceImpl
 * @PackageName: com.xmall.service.admin
 * @Description: TODO
 * @date 2020/12/06
 * @Version 1.0
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public CommentVO queryCommentList(Integer page, Integer limit, Integer userId, Integer valueId, String sort, String order) {
        PageHelper.startPage(page, limit);

        CommentExample commentExample = new CommentExample();
        commentExample.setOrderByClause(sort + " " + order);

        CommentExample.Criteria criteria = commentExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        criteria.andDeletedEqualTo(false);
        List<Comment> commentList = commentMapper.selectByExample(commentExample);
        CommentVO<Comment> commentVO = new CommentVO<>();
        commentVO.setItems(commentList);

        PageInfo<Comment> commentPageInfo = new PageInfo<>();
        commentVO.setTotal((int) commentPageInfo.getTotal());
        return commentVO;
    }

    @Override
    public void deleteComment(Comment comment) {
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andIdEqualTo(comment.getId());
        Comment comment1 = new Comment();
        comment1.setDeleted(true);
        commentMapper.updateByExampleSelective(comment1, commentExample);
    }

    @Override
    public Map<String, Long> countComment(Integer valueId, Byte type) {
        return commentMapper.countComment(valueId, type);
    }

    @Override
    public MallCommentVO listComment(Map<String, String> map) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Map<String, Long> commentMap = commentMapper.countComment(Integer.parseInt(map.get("valueId")),
                Byte.parseByte(map.get("type")));
        ArrayList<MallCommentDataVO> mallCommentDataVOS = new ArrayList<>();
        int currentPage = Integer.parseInt(map.get("page"));
        PageHelper.startPage(currentPage,Integer.parseInt(map.get("size")));
        CommentExample example = new CommentExample();
        CommentExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(Integer.parseInt(map.get("valueId")));
        criteria.andTypeEqualTo(Byte.parseByte(map.get("type")));
        criteria.andHasPictureEqualTo(Integer.parseInt(map.get("showType")) == 1 ? true : false);
        criteria.andDeletedEqualTo(false);
        List<Comment> commentList = commentMapper.selectByExample(example);
        for (Comment comment : commentList) {
            Map<String, String> userInfoMap = new HashMap<>(16);
            // TODO
            userInfoMap.put("nickName", user.getNickname());
            userInfoMap.put("avatarUrl", user.getAvatar());
            mallCommentDataVOS.add(new MallCommentDataVO(userInfoMap,
                    comment.getAddTime(),
                    comment.getContent(),
                    comment.getPicUrls()));
        }
        int count = commentMap.get("allCount") != null ? commentMap.get("allCount").intValue() : 0;
        return new MallCommentVO(count, currentPage, mallCommentDataVOS);
    }
}
